package linuxbox.reservation.logic;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.TimeZone;
import java.text.SimpleDateFormat;
import java.util.Locale;

import linuxbox.BoxStatic;
import linuxbox.util.StringUtil;

public class BoxReservationEntry
{
  protected int m_iID;
  protected Timestamp m_FromTime;
  protected Timestamp m_ToTime;
	protected	String m_sUserID = "";
	protected String m_sSubject = "";
	protected String m_sDescription = "";
  protected int m_iResourceID = 0;
  protected boolean m_bRepeat = false;
  protected boolean m_bIsAllDay = false;

  public void copyFrom(BoxReservationEntry entry)
  {
    m_iID = entry.getID();
    m_iResourceID = entry.getResourceID();
    m_bRepeat = entry.getRepeat();
    m_bIsAllDay = entry.getIsAllDay();

    if (entry.getUserID() == null)
      m_sUserID = null;
    else
      m_sUserID = new String(entry.getUserID());
    if (entry.getSubject() == null)
      m_sSubject = null;
    else
      m_sSubject = new String(entry.getSubject());
    if (entry.getDescription() == null)
      m_sDescription = null;
    else
      m_sDescription = new String(entry.getDescription());
    if (entry.getFromTime() == null)
      m_FromTime = null;
    else
      m_FromTime = new Timestamp(entry.getFromTime().getTime());
    if (entry.getToTime() == null)
      m_ToTime = null;
    else
      m_ToTime = new Timestamp(entry.getToTime().getTime());
  }

  public int getID()
	{
		return m_iID;
	}

  public void setID(int id)
	{
	  m_iID = id;
	}

  public String getUserID()
	{
		return m_sUserID;
	}

  public void setUserID(String s)
	{
    m_sUserID = s;
	}

  public Timestamp getFromTime()
	{
		return m_FromTime;
	}

  public void setFromTime(Timestamp tm)
	{
    m_FromTime = tm;
	}

  public Timestamp getToTime()
	{
		return m_ToTime;
	}

  public void setToTime(Timestamp tm)
	{
    m_ToTime = tm;
	}

  public String getSubject()
	{
    return m_sSubject;
	}
  public void setSubject(String s)
	{
    m_sSubject = s;
	}

  public String getDescription()
	{
    return m_sDescription;
	}
  public void setDescription(String s)
	{
    m_sDescription = s;
	}

  public int getResourceID()
  {
    return m_iResourceID;
  }
  public void setResourceID(int i)
  {
    m_iResourceID = i;
  }

  public boolean getIsAllDay()
  {
    return m_bIsAllDay;
  }
  public void setIsAllDay(boolean b)
  {
    m_bIsAllDay = b;
  }

  public boolean getRepeat()
  {
    return m_bRepeat;
  }
  public void setRepeat(boolean b)
  {
    m_bRepeat = b;
  }

  public int getFromHour(TimeZone tz)
  {
	  Calendar tmpTime = Calendar.getInstance(tz);
	  tmpTime.setTime(m_FromTime);
  	return tmpTime.get(Calendar.HOUR_OF_DAY);
  }

  public int getToHour(TimeZone tz)
  {
	  Calendar tmpTime = Calendar.getInstance(tz);

	  if ( m_ToTime != null )
	  {
		  tmpTime.setTime(m_ToTime);
		  int toHour = tmpTime.get(Calendar.HOUR_OF_DAY);

		  if (( (tmpTime.getTime().getTime())%(60*60*1000)) >=60*1000) //greater than 1 min
		    toHour ++;
		  return toHour;
	  }
	  else
  		tmpTime.setTime(m_FromTime);
  	return tmpTime.get(Calendar.HOUR_OF_DAY);
  }

  public String getTimeRangeString(TimeZone tz)
  {
    //TODO: put string in resource
    if (m_bIsAllDay)
      return "All day";

	  if (m_FromTime == null || m_ToTime == null)
		  return "";
    SimpleDateFormat dateTimeFormatter = new SimpleDateFormat(BoxStatic.SHORT_TIME_FORMAT, Locale.US);
    Calendar cal = Calendar.getInstance(tz);
    dateTimeFormatter.setCalendar(cal);

	  StringBuffer s = new StringBuffer();
	  s.append(dateTimeFormatter.format(m_FromTime));
    s.append("-");
    s.append(dateTimeFormatter.format(m_ToTime));
    return s.toString();
  }

  public String getDateString(TimeZone tz)
  {
    return StringUtil.getDateTimeFormatString(tz, BoxStatic.SHORT_DATE_FORMAT, m_FromTime );
  }

  public String toString()
  {
    String s = "id=" + m_iID
                + ", m_FromTime=" + m_FromTime
                + ", m_ToTime=" + m_ToTime
	              + ", m_sUserID=" + m_sUserID
	              + ", m_sSubject=" + m_sSubject
	              + ", m_sDescription=" + m_sDescription
	              + ", m_iResourceID=" + m_iResourceID
	              + ", m_bRepeat=" + m_bRepeat
	              + ", m_bIsAllDay=" + m_bIsAllDay
                + "\n";
    return s;
  }
}
